#Author: Tanyong
# *-* coding:utf8 *-*
# @time       : 2018/7/23 20:59
# @Author     : Tanyong
# @Site       : 
# @File       : image_storage.py
# @Software   : PyCharm
import logging
from qiniu import Auth, put_data

access_key = 'x0UnDICxnSTFRkoqcZ-Sk0QPDGHgPMWyznVq7NNk'
secret_key = 'r30hI59WtckKerxAnUlSfuxme0jUWSV_GLl9sgZO'
bucket_name = 'stayhungery'

def image_storage(data):
    if not data:
        return None

    try:
        q = Auth(access_key, secret_key)
        token = q.upload_token(bucket_name)
        ret, info = put_data(token, None, data)
    except Exception as e:
        logging.error(e)
        raise e

    if info and info.status_code != 200:
        raise Exception("上传文件到七牛失败")

    # print(ret, info)

    # 返回七牛中保存的图片名，这个图片名也是访问七牛获取图片的路径
    return ret["key"]

if __name__ == '__main__':
    file_name = input("输入上传的文件")
    with open(file_name, "rb") as f:
        image_storage(f.read())

